package com.softbank.hwbanner;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class BaseIndicator extends View implements IIndicator {
    private String TAG = "BaseIndicator";
    private int mSize = 0;
    private int mCurrentItem = 0;
    private boolean mIsScrollingRight = true;
    private double mPercent = 0f;
    private boolean mIsScrollingBack = false;
    private int mColor = Color.WHITE;

    public BaseIndicator(Context context) {
        super(context);
    }

    public BaseIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public int getSize() {
        return mSize;
    }

    @Override
    public void setSize(int size) {
        mSize = size;
        invalidate();
    }

    @Override
    public void setCurrentItem(int current) {
        mCurrentItem = current;
    }

    @Override
    public void setOffset(int visibleIndex, double percent) {
        mIsScrollingRight = visibleIndex == mCurrentItem;//表示整体趋势是向右滑动
        mIsScrollingBack = percent < mPercent;//如果比先前的小，表明正在回退正常位置
        mPercent = percent;
        invalidate();
    }

    public void setColor(int color) {
        mColor = color;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mSize > 0) {
            double density = getResources().getDisplayMetrics().density;
            int length = (int) (density * 15);//40dp
            int offset = (getMeasuredWidth() - length * (mSize - 1)) / 2;
            //绘制指示器
            for (int i = 0; i < mSize; i++) {
                if (mIsScrollingRight) {
                    //整体已经偏离正常位置向右滑动
                    if (i == mCurrentItem) {
                        Log.e(TAG, "");
                        drawSingleIndicator(canvas, offset, 1 - mPercent);
                    } else if (i == mCurrentItem + 1) {
                        drawSingleIndicator(canvas, offset, mPercent);
                    } else {
                        drawSingleIndicator(canvas, offset, 0);
                    }

                    if (mIsScrollingBack) {
                        //从偏离位置回退到正常位置
                    } else {
                        //滑动
                    }
                } else {
                    if (i == mCurrentItem - 1) {
                        Log.e(TAG, "");
                        drawSingleIndicator(canvas, offset, 1 - mPercent);
                    } else if (i == mCurrentItem) {
                        drawSingleIndicator(canvas, offset, mPercent);
                    } else {
                        drawSingleIndicator(canvas, offset, 0);
                    }
                    //向左滑动
                    if (mIsScrollingBack) {
                        //从偏离位置回退到正常位置
                    } else {
                        //滑动
                    }
                }
                offset += length;
                Log.e(TAG, "onDraw: " + mPercent + "|" + mCurrentItem);
            }

        }
    }

    /**
     * 绘制单独版本
     */
    private void drawSingleIndicator(Canvas canvas, int offset, double percent) {
        double density = getResources().getDisplayMetrics().density;
        float radio = (float) (density * 3);
        Paint paint = new Paint();
        paint.setColor(mColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setAntiAlias(true);
        canvas.drawCircle(offset, getMeasuredHeight() / 2, (float) (radio * (1 + percent * 0.5)), paint);
    }
}
